package bookstore_mall.controller;

import bookstore_mall.domain.dto.user.LoginRequest;
import bookstore_mall.domain.dto.user.RegisterRequest;
import bookstore_mall.domain.dto.user.UserInfoResponse;
import bookstore_mall.resp.ResponseResult;
import bookstore_mall.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "用户接口")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param registerRequest 注册请求
     * @return 用户信息
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public ResponseResult<Boolean> register(@RequestBody RegisterRequest registerRequest) {
        return ResponseResult.success(userService.register(registerRequest));
    }

    /**
     * 用户登录
     * @param loginRequest 登录请求
     * @return token
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public ResponseResult<String> login(@RequestBody LoginRequest loginRequest) {
        return ResponseResult.success(userService.login(loginRequest));
    }

    /**
     * 获取当前登录用户信息
     * @return 用户信息
     */
    @GetMapping("/info")
    @ApiOperation(value = "获取当前登录用户信息")
    public ResponseResult<UserInfoResponse> getCurrentUserInfo() {
        return ResponseResult.success(userService.getCurrentUserInfo());
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    @ApiOperation(value = "退出登录")
    public ResponseResult<Void> logout() {
        userService.logout();
        return ResponseResult.success();
    }
} 